# ===========================
# 🧩 VIBE CODING Makefile
# ===========================
# 把复杂命令变成一行"自然语言"的入口

.PHONY: help install lint type test quick audit clean dev deploy fix-apply verify-precommit check-contract

# 默认目标
help:
	@echo "🧩 VIBE CODING - OPC Monitor 自动化命令"
	@echo ""
	@echo "📦 环境管理:"
	@echo "  make install       - 安装所有依赖 + pre-commit 钩子"
	@echo "  make clean         - 清理缓存和临时文件"
	@echo ""
	@echo "🧹 代码质量:"
	@echo "  make lint          - 运行 Ruff 代码检查"
	@echo "  make type          - 运行 MyPy 类型检查"
	@echo "  make test          - 运行 Pytest 测试"
	@echo "  make quick         - 快速检查 (lint + type + test)"
	@echo "  make check-contract - 检查 OpenAPI 契约漂移"
	@echo ""
	@echo "🤖 自动化审计:"
	@echo "  make audit         - 完整审计流程 (AI 自诊断)"
	@echo "  make docs          - 生成自动文档"
	@echo "  make fix-apply     - 应用 AI 修复建议 (dry-run)"
	@echo "  make fix-apply-real - 实际应用 AI 修复"
	@echo ""
	@echo "🔍 Pre-commit 管理:"
	@echo "  make verify-precommit - 验证 pre-commit 配置"
	@echo "  make precommit-run    - 手动运行 pre-commit"
	@echo ""
	@echo "🚀 运行:"
	@echo "  make dev           - 启动开发服务器"
	@echo "  make deploy        - 部署到生产环境"

# 📦 安装依赖
install:
	@echo "📦 安装 Python 依赖..."
	pip install -r requirements.txt
	@echo "🔧 安装开发工具..."
	pip install pre-commit ruff mypy pytest black isort pyyaml
	@echo "🔒 安装 pre-commit 钩子..."
	pre-commit install
	@echo "✅ 安装完成！"

# 🧹 代码检查
lint:
	@echo "🧹 运行 Ruff 检查..."
	ruff check backend/app

type:
	@echo "🔍 运行 MyPy 类型检查..."
	mypy backend/app --strict

test:
	@echo "🧪 运行 Pytest 测试..."
	pytest tests -v --tb=short

# ⚡ 快速检查 (pre-commit 轻量版)
quick:
	@echo "⚡ 快速检查开始..."
	@echo "1️⃣ Ruff 检查..."
	@ruff check backend/app || true
	@echo "2️⃣ MyPy 类型检查..."
	@mypy backend/app --strict || true
	@echo "3️⃣ Pytest 快速测试..."
	@pytest -q --maxfail=1 --disable-warnings || true
	@echo "✅ 快速检查完成！"

# 🤖 完整审计流程
audit:
	@echo "🤖 运行完整审计流程..."
	@echo "1️⃣ 运行代码检查..."
	@$(MAKE) quick
	@echo "2️⃣ 触发 AI 自诊断..."
	@python .precommit/ai/trigger.py || true
	@echo "3️⃣ 生成审计报告..."
	@python .vibe/scripts/auto_audit.py || true
	@echo "4️⃣ 汇总结果..."
	@python .vibe/scripts/summarize_findings.py || true
	@echo "✅ 审计完成！查看 .precommit/outputs/ 目录"

# 📚 生成文档
docs:
	@echo "📚 生成自动文档..."
	@python scripts/generate_docs.py
	@echo "✅ 文档生成完成！查看 docs/auto_prompt_docs/"

# 🧽 清理缓存
clean:
	@echo "🧽 清理缓存和临时文件..."
	find . -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true
	find . -type d -name ".pytest_cache" -exec rm -rf {} + 2>/dev/null || true
	find . -type d -name ".mypy_cache" -exec rm -rf {} + 2>/dev/null || true
	find . -type d -name ".ruff_cache" -exec rm -rf {} + 2>/dev/null || true
	find . -type f -name "*.pyc" -delete 2>/dev/null || true
	find . -type f -name "*.pyo" -delete 2>/dev/null || true
	@echo "✅ 清理完成！"

# 🚀 开发服务器
dev:
	@echo "🚀 启动开发服务器..."
	cd backend && uvicorn app.main:app --reload --host 0.0.0.0 --port 8000

# 📋 OpenAPI 契约检测
check-contract:
	@echo "📋 检查 OpenAPI 契约漂移..."
	@python scripts/check_openapi_drift.py

# 🔧 应用 AI 修复
fix-apply:
	@echo "🔧 应用 AI 修复建议 (Dry Run)..."
	@python .precommit/ai/fixer.py --report .precommit/outputs/reports/ai_diagnostic_latest.json
	@echo "💡 使用 'make fix-apply-real' 实际应用修复"

fix-apply-real:
	@echo "🔧 实际应用 AI 修复..."
	@python .precommit/ai/fixer.py --report .precommit/outputs/reports/ai_diagnostic_latest.json --apply
	@echo "✅ 修复已应用！"

# 🔍 Pre-commit 管理
verify-precommit:
	@echo "🔍 验证 pre-commit 配置..."
	@python .precommit/checkers/tool_checker.py
	@echo "📋 Pre-commit 钩子状态:"
	@pre-commit --version || echo "❌ pre-commit 未安装"
	@test -f .git/hooks/pre-commit && echo "✅ pre-commit 钩子已安装" || echo "❌ pre-commit 钩子未安装"
	@echo "📄 Pre-commit 配置文件:"
	@test -f .pre-commit-config.yaml && echo "✅ .pre-commit-config.yaml 存在" || echo "❌ .pre-commit-config.yaml 不存在"
	@echo "📂 Prompt 索引:"
	@test -f .precommit/prompts/index.yaml && echo "✅ index.yaml 存在" || echo "❌ index.yaml 不存在"

precommit-run:
	@echo "🔍 手动运行 pre-commit..."
	@pre-commit run --all-files

# 🏭 部署
deploy:
	@echo "🏭 部署到生产环境..."
	@echo "⚠️  请确保已经通过审计！"
	@$(MAKE) audit
	@echo "🚀 启动生产服务器..."
	cd backend && uvicorn app.main:app --host 0.0.0.0 --port 8000
